/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.menu;

import java.util.Vector;

import nl.biz_e.gwt.baselib.client.i18n.Constants;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Widget;

/**
 * Default implementation of {@link IMenuItem} implementing all basic
 * functionality
 * 
 * @author Joost A. Bloemsma
 */
public class MenuItem implements ClickHandler, IMenuItem {
	private String text;
	private String href = "#";
	private boolean isAnchor = false;
	private Anchor widget = null;
	private boolean alwaysVisible = false;
	private boolean selected = false;
	private final Vector<MenuItemClickedListener> listeners = new Vector<MenuItemClickedListener>();
	private IMenuPanelManager manager;
	
	/**
	 * Constructs a new MenuItem instance with a default label
	 */
	public MenuItem() {
		this(Constants.getBaseLib().menuItem());
	}
	
	/**
	 * Constructs a new MenuItem instance with the passed label
	 * 
	 * @param text
	 */
	public MenuItem(String text) {
		this(text, false);
	}
	
	/**
	 * Constructs a new MenuItem instance
	 * 
	 * @param text
	 * @param alwaysVisible
	 */
	public MenuItem(String text, boolean alwaysVisible) {
		this(text, "#", alwaysVisible);
	}
	
	/**
	 * Constructs a new MenuItem instance
	 * 
	 * @param text
	 *        the label
	 * @param href
	 *        the link to open when clicked
	 */
	public MenuItem(String text, String href) {
		this(text, href, false);
	}
	
	/**
	 * Constructs a new MenuItem instance
	 * 
	 * @param text
	 * @param href
	 * @param alwaysVisible
	 */
	public MenuItem(String text, String href, boolean alwaysVisible) {
		this.alwaysVisible = alwaysVisible;
		this.setHRef(href);
		this.setText(text);
	}
	
	/**
	 * Constructs a new MenuItem instance
	 * 
	 * @param text
	 *        the label
	 * @param listener
	 *        the {@link IMenuItem.MenuItemClickedListener} to fire when
	 *        clicked
	 */
	public MenuItem(String text, MenuItemClickedListener listener) {
		this(text, listener, false);
	}
	
	/**
	 * Constructs a new MenuItem instance
	 * 
	 * @param text
	 * @param listener
	 * @param alwaysVisible
	 */
	public MenuItem(String text, MenuItemClickedListener listener,
			boolean alwaysVisible) {
		this.setText(text);
		this.alwaysVisible = alwaysVisible;
		this.addListener(listener);
	}
	
	/**
	 * Sets the link to open when clicked
	 * 
	 * @param href
	 */
	public void setHRef(String href) {
		this.href = href;
		this.isAnchor = href != null ? !href.equals("#") : false;
		if (this.widget != null) {
			widget.setHref(href);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addListener(MenuItemClickedListener listener) {
		if (listener != null)
			listeners.add(listener);
	}
	
	/**
	 * {@inheritDoc}
	 */
	/**
	 * {@inheritDoc}
	 */
	public Widget getWidget() {
		if (widget == null) {
			Anchor a = new Anchor(this.getText(), "#");
			if (this.isAnchor) {
				a.setHref(this.getHref());
			} else {
				a.addClickHandler(this);
			}
			widget = a;
		}
		return widget;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getText() {
		return text;
	}
	
	/**
	 * @param text
	 *        the text to set
	 */
	public void setText(String text) {
		this.text = text;
		if (this.widget != null) {
			widget.setText(text);
		}
	}
	
	/**
	 * @return the href
	 */
	public String getHref() {
		return href;
	}
	
	/**
	 * @return the isAnchor
	 */
	public boolean isAnchor() {
		return isAnchor;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(ClickEvent event) {
		for (MenuItemClickedListener listener : listeners) {
			if (listener != null)
				listener.menuItemClicked(this);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean isAlwaysVisible() {
		return alwaysVisible;
	}
	
	/**
	 * @param alwaysVisible
	 *        the alwaysVisible to set
	 */
	public void setAlwaysVisible(boolean alwaysVisible) {
		this.alwaysVisible = alwaysVisible;
	}
	
	/**
	 * @return the selected
	 */
	public boolean isSelected() {
		return selected;
	}
	
	/**
	 * @param selected
	 *        the selected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
		if (manager != null)
			manager.setMenuItemSelected(this, selected);
	}
	
	/**
	 * @return the manager
	 */
	public IMenuPanelManager getManager() {
		return manager;
	}
	
	/**
	 * @param manager
	 *        the manager to set
	 */
	public void setManager(IMenuPanelManager manager) {
		this.manager = manager;
	}
}
